import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class FlutterToast {
  // 私有构造函数
  FlutterToast._();

  /// 显示普通提示
  static void show(
    String message, {
    Toast length = Toast.LENGTH_SHORT,
    ToastGravity gravity = ToastGravity.BOTTOM,
    Color backgroundColor = const Color(0xAA000000),
    Color textColor = Colors.white,
    double fontSize = 16.0,
    bool webShowClose = false,
    bool webBgColor = false,
    int timeInSecForIosWeb = 1,
  }) {
    Fluttertoast.showToast(
      msg: message,
      toastLength: length,
      gravity: gravity,
      backgroundColor: backgroundColor,
      textColor: textColor,
      fontSize: fontSize,
      webShowClose: webShowClose,
      webBgColor: webBgColor,
      timeInSecForIosWeb: timeInSecForIosWeb,
    );
  }

  /// 显示成功提示
  static void success(
    String message, {
    Toast length = Toast.LENGTH_SHORT,
    ToastGravity gravity = ToastGravity.BOTTOM,
    double fontSize = 16.0,
  }) {
    Fluttertoast.showToast(
      msg: message,
      toastLength: length,
      gravity: gravity,
      backgroundColor: _getColorByType(ToastType.success),
      textColor: Colors.white,
      fontSize: fontSize,
    );
  }

  /// 显示错误提示
  static void error(
    String message, {
    Toast length = Toast.LENGTH_LONG,
    ToastGravity gravity = ToastGravity.CENTER,
    double fontSize = 16.0,
  }) {
    Fluttertoast.showToast(
      msg: message,
      toastLength: length,
      gravity: gravity,
      backgroundColor: _getColorByType(ToastType.error),
      textColor: Colors.white,
      fontSize: fontSize,
    );
  }

  /// 显示警告提示
  static void warning(
    String message, {
    Toast length = Toast.LENGTH_SHORT,
    ToastGravity gravity = ToastGravity.BOTTOM,
    double fontSize = 16.0,
  }) {
    Fluttertoast.showToast(
      msg: message,
      toastLength: length,
      gravity: gravity,
      backgroundColor: _getColorByType(ToastType.warning),
      textColor: Colors.black87,
      fontSize: fontSize,
    );
  }

  /// 显示信息提示
  static void info(
    String message, {
    Toast length = Toast.LENGTH_SHORT,
    ToastGravity gravity = ToastGravity.BOTTOM,
    double fontSize = 16.0,
  }) {
    Fluttertoast.showToast(
      msg: message,
      toastLength: length,
      gravity: gravity,
      backgroundColor: _getColorByType(ToastType.info),
      textColor: Colors.white,
      fontSize: fontSize,
    );
  }

  /// 自定义Toast
  static void custom({
    required String message,
    Toast length = Toast.LENGTH_SHORT,
    ToastGravity gravity = ToastGravity.BOTTOM,
    Color? backgroundColor,
    Color? textColor,
    double fontSize = 16.0,
    bool webShowClose = false,
    bool webBgColor = false,
    int timeInSecForIosWeb = 1,
  }) {
    Fluttertoast.showToast(
      msg: message,
      toastLength: length,
      gravity: gravity,
      backgroundColor: backgroundColor ?? const Color(0xAA000000),
      textColor: textColor ?? Colors.white,
      fontSize: fontSize,
      webShowClose: webShowClose,
      webBgColor: webBgColor,
      timeInSecForIosWeb: timeInSecForIosWeb,
    );
  }

  /// 取消所有Toast
  static void cancel() {
    Fluttertoast.cancel();
  }

  // 根据类型获取颜色
  static Color _getColorByType(ToastType type) {
    switch (type) {
      case ToastType.success:
        return Colors.green;
      case ToastType.error:
        return Colors.red;
      case ToastType.warning:
        return Colors.orange;
      case ToastType.info:
        return Colors.blue;
      default:
        return const Color(0xAA000000);
    }
  }
}

enum ToastType { success, error, warning, info }
